package com.sxm.yiwei.tools;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.widget.Toast;

import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.AppManager;
import com.sxm.yiwei.activitis.HotSendActivity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by KuKa on 2017/9/8.
 * 相机相册管理类
 */

public class BaseIntents {
    private static final int PHOTO_TEXT = 3022;
    private static final int PHOTO_PICKED_WITH_DATA = 3021;
    /*用来标识请求照相功能的activity*/
    private static final int CAMERA_WITH_DATA = 3023;
    public File mCurrentPhotoFile;
    private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");


    // 以当前时间给图片命名
    public String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMddHHmmss");
        return dateFormat.format(date) + ".jpg";
    }

    public void doTakePhoto() {
        try {
            /*  int REQUEST_EXTERNAL_STORAGE = 1;
             String[] PERMISSIONS_STORAGE = {
                    android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                     Manifest.permission.CAMERA
            };
            int permission = ActivityCompat.checkSelfPermission(AppManager.getAppManager().currentActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // We don't have permission so prompt the user
                ActivityCompat.requestPermissions(
                        AppManager.getAppManager().currentActivity(),
                        PERMISSIONS_STORAGE,
                        REQUEST_EXTERNAL_STORAGE
                );
            }*/
            // Launch camera to take photo for selected contact
            PHOTO_DIR.mkdirs();// 创建照片的存储目录
            // 给新照的照片文件命名
            mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());
            final Intent intent = getTakePickIntent(mCurrentPhotoFile);
            AppManager.getAppManager().currentActivity().startActivityForResult(intent, CAMERA_WITH_DATA);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(AppManager.getAppManager().currentActivity(), R.string.photoPickerNotFoundText,
                    Toast.LENGTH_LONG).show();
        }
    }
    // 封装请求Gallery的intent
    public static Intent getPhotoPickIntent() {
        Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
        return intent;
    }

        // 相机拍照
    public Intent getTakePickIntent(File f) {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri uri11 = FileProvider.getUriForFile(AppManager.getAppManager().currentActivity(), AppManager.getAppManager().currentActivity().getApplicationContext().getPackageName() + ".provider", f);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,uri11);
            //  大于等于24即为7.0及以上执行内容
        } else {
            //  低于24即为7.0以下执行内容
            intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(f));
        }
        return intent;
    }

    // 请求Gallery程序
    public void doPickPhotoFromGallery() {
        try {
            // Launch picker to choose photo for selected contact
            final Intent intent = getPhotoPickIntent();
            AppManager.getAppManager().currentActivity().startActivityForResult(intent, PHOTO_TEXT);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(AppManager.getAppManager().currentActivity(), R.string.photoPickerNotFoundText1,
                    Toast.LENGTH_LONG).show();
        }

    }
    public void doCropPhoto(File f) {
        try {
            // 启动gallery去剪辑这个照片
            final Intent intent;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Uri uriForFile = FileProvider.getUriForFile(AppManager.getAppManager().currentActivity(), AppManager.getAppManager().currentActivity().getApplicationContext().getPackageName() + ".provider", f);
                intent = getCropImageIntent(uriForFile);

                //  大于等于24即为7.0及以上执行内容
            } else {
                //  低于24即为7.0以下执行内容
                intent = getCropImageIntent(Uri.fromFile(f));

            }
            AppManager.getAppManager().currentActivity().startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
        } catch (Exception e) {
            Toast.makeText(AppManager.getAppManager().currentActivity(), R.string.photoPickerNotFoundText,
                    Toast.LENGTH_LONG).show();
        }
    }
    /**
     * Constructs an intent for image cropping. 调用图片剪辑程序
     */
    public static Intent getCropImageIntent(Uri photoUri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(photoUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        return intent;
    }
}
